package edu.hawaii.duedates;

import java.util.ArrayList;
import java.util.List;

import edu.hawaii.duedates.libraries.HSLLibrary;
import edu.hawaii.duedates.libraries.UHMLibrary;

/**
 * DueDates.class
 * DueDates is a program that enables a user the ability to check the 
 * status of items they may have borrowed from one or more library systems. 
 * 
 * @author Anthony Du, Vincent Leung
 */
public final class DueDates {

  private static DueDates _instance = new DueDates();

  /**
   * Constructor for Singleton.
   */
  private DueDates() {
    //construct object
  }

  /**
   * Part of the Singleton.
   * @return _instance.
   */
  public static DueDates getInstance() {
    return _instance;
  }

  /**
   * Main method to call our logger and library functions.
   * @param args The log in information needed.
   * @throws Exception if there is an error.
   */
  public static void main(String[] args) throws Exception {
    // Instantiate the error logger.
    ErrorLogger logger = new ErrorLogger(args);
    // Instantiate a List containing Libraries.
    List<Library> libraries = new ArrayList<Library>();

    // Start of try/catch block.
    try {
      // Extract true params (without logging options).
      String[] params = logger.getParams();
      // Parse out the necessary parameters and build libraries
      for (int i = 0; i < params.length; i += 3) {
        if (params[i].equals("-uhm")) {
          libraries.add(new UHMLibrary("UH Manoa Library", params[i + 1], params[i + 2])); 
        } 
        else if (params[i].equals("-hsl")) {
          libraries.add(new HSLLibrary("Hawaii State Library", params[i + 1], params[i + 2]));
        } 
        else {
          System.err.println("You have entered an unsupported library system. Please revise.");
          break;
        }
      }
      for (Library lib : libraries) {
        String libName = lib.getLibName();
        System.out.printf("--Getting data from %s--%n", libName);
        String libInfo = lib.getLibInfo();
        System.out.printf("Results for %s:%n %s%n", libName, libInfo);
      }
    }
    catch (Exception e) {
      logger.logError(e);
    }
    finally {
      System.out.println("--Connection to closed.--");
    }
  }
}
